/*
 * Copyright (C) 2008 Juan Alberto López Cavallotti
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package pantallas;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import juego.Parametros;
import juego.herramientasGui.*;

/**
 *
 * @author juan
 */
public class AreaJuego {
    private int anchoPixeles;
    private int altoPixeles;
    
    private int alto;
    private int ancho;
    
    private int x;
    private int y;
    
    private int anchoCuadro;
    private int altoCuadro;
    
    Image img;
    
    Bloque[][] bloques;

    public AreaJuego(int anchoPixeles, int altoPixeles, int x, int y) {
        System.out.println("Obteniendo el alto y el ancho del juego");
        alto = Parametros.getInstancia().getAltoBloques();
        ancho = Parametros.getInstancia().getAnchoBloques();
        
        this.anchoPixeles = anchoPixeles;
        this.altoPixeles = altoPixeles;
        this.x = x;
        this.y = y;
        
        //calcular las dimensiones del dibujo
        anchoCuadro = (int)  anchoPixeles / ancho; 
        altoCuadro = (int) altoPixeles / alto;
        
        //crear el lugar donde se dibuja
        img = Image.createImage(ancho*anchoCuadro, alto*altoCuadro);
        
    }

    public void setBloques(Bloque[][] bloques) {
        //System.out.println("Llamada a set bloques");
        this.bloques = bloques;
    }
    
    public void pintar(Graphics g) {
        
        //crear la imagen del area
        paint(img.getGraphics());
        //dibujar la imagen
        g.drawImage(img, x, y, 0);
        
        //dibujar un recuadro al rededor
        g.setColor(0,210,0);
        g.drawRect(x-1, y-1, ancho*anchoCuadro+1, alto*altoCuadro+1);
    }
    
    public void paint(Graphics g)
    {
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, ancho*anchoCuadro, alto*altoCuadro);
        
        if (bloques == null) //si no hay bloques no se hace nada
            return;

        
        boolean dibujarCabeza = false;
        int iCab = 0;
        int jCab = 0;
        
        for (int i = 0; i < alto; i++)
        {
            for (int j = 0; j < ancho; j++)
            {
                decodificarColor(bloques[i][j], g);
                //dibujar el cuadro
                if (bloques[i][j] instanceof BloqueObjetivo)
                {
                    g.fillRoundRect(j*anchoCuadro, i*altoCuadro, anchoCuadro, altoCuadro, anchoCuadro, altoCuadro);
                }else if (bloques[i][j] instanceof BloqueCabezaVibora)
                {
                    //retrasar el dibujo de la cabeza hasta el fin del loop
                    iCab = i;
                    jCab = j;
                    dibujarCabeza = true;
                    //dibujarCabeza((BloqueCabezaVibora) bloques[i][j], g, x+j*anchoCuadro, y+i*altoCuadro, anchoCuadro, altoCuadro);
                }else if (bloques[i][j] instanceof BloqueVibora)
                {
                    g.fillRect(j*anchoCuadro, i*altoCuadro, anchoCuadro, altoCuadro);
                }
            }
        }
        
        if (dibujarCabeza)
        {
            decodificarColor(bloques[iCab][jCab], g);
            
            try {
            dibujarCabeza((BloqueCabezaVibora) bloques[iCab][jCab], g, jCab*anchoCuadro, iCab*altoCuadro, anchoCuadro, altoCuadro);
            } catch (ClassCastException ex) {
                System.out.println("i: "+iCab+", j: "+jCab);
            }
            
            dibujarCabeza = false;
        }
    }
    private void decodificarColor(Bloque bloque, Graphics g) {
        if (bloque instanceof BloqueFondo)
            g.setColor(0, 0, 0); //negro
        else if (bloque instanceof BloqueVibora)
            g.setColor(255,0,0); //rojo
        else if (bloque instanceof BloqueObjetivo)
            g.setColor(180,180,180); //blanco
        else if (bloque instanceof BloqueLimite)
            g.setColor(0, 0, 255); //azul
        else
            g.setColor(0, 0, 0); //negro
    }

    private void dibujarCabeza(BloqueCabezaVibora bloque, Graphics g, int x, int y, int ancho, int alto) {
        int direccion = bloque.getDireccion();
        
        
        int x1 = 0, x2 = 0, y1 = 0, y2 = 0, z1 = 0, z2 = 0;
        
        int x1ojo = 0, x2ojo = 0, y1ojo = 0, y2ojo = 0;
         
        switch (direccion)
        {
            case BloqueCabezaVibora.ABAJO:
                x1 = x;
                x2 = y;
                y1 = x+ancho;
                y2 = y;
                z1 = x+ancho/2;
                z2 = y+alto;
                x1ojo = ancho/4;
                x2ojo = 0;
                y1ojo = (3*ancho)/4;
                y2ojo = 0;
                break;
            case BloqueCabezaVibora.ARRIBA:
                x1 = x+ancho/2;
                x2 = y;
                y1 = x+ancho;
                y2 = y+alto;
                z1 = x;
                z2 = y+alto;
                x1ojo = ancho/4;
                x2ojo = alto;
                y1ojo = (3*ancho)/4;
                y2ojo = alto;
                break;
            case BloqueCabezaVibora.IZQUIERDA:
                x1 = x+ancho;
                x2 = y;
                y1 = x+ancho;
                y2 = y+alto;
                z1 = x;
                z2 = y+alto/2;
                x1ojo = ancho;
                x2ojo = alto/4;
                y1ojo = ancho;
                y2ojo = (3*alto)/4;
                break;
            case BloqueCabezaVibora.DERECHA:
                x1 = x;
                x2 = y;
                y1 = x+ancho;
                y2 = y+alto/2;
                z1 = x;
                z2 = y+alto;
                x1ojo = 0;
                x2ojo = alto/4;
                y1ojo = 0;
                y2ojo = (3*alto)/4;
                break;
        }
        
        
        
        //ojitos escalables
        int radioOjitos = (int) Math.floor((double)anchoPixeles/(double)(ancho*4)+1);
        //radioOjitos -= 1;   
        int diamOjitos = radioOjitos*2;
        
        
        g.fillTriangle(x1, x2 ,y1 ,y2 ,z1 ,z2 );
        g.setColor(200,200,200);
        g.fillArc(x+x1ojo-radioOjitos, y+x2ojo-radioOjitos, diamOjitos, diamOjitos, 0, 360);
        g.fillArc(x+y1ojo-radioOjitos, y+y2ojo-radioOjitos, diamOjitos, diamOjitos, 0, 360);
        g.setColor(0, 0, 0);
        g.fillRect(x+x1ojo, y+x2ojo, 1, 1);
        g.fillRect(x+y1ojo, y+y2ojo, 1, 1);        
    }

    public int getWidth() {
        return anchoPixeles;
    }

    public int getHeight() {
        return altoPixeles;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
    
    
}
